package com.rui.net.http;

import com.rui.net.ITask;
import com.rui.net.ITaskListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpTask extends Thread implements ITask {
    private HttpClient client;
    private byte[] content;
    private boolean isException = false;
    private boolean isStop = false;
    private boolean isTimeout = false;
    private CountDownLatch latch;
    private ITaskListener listener;
    private String sessionId;
    private int timeOut = 0;
    private String url;

    public void execute(int paramInt, String paramString, ITaskListener paramITaskListener) {
        execute(30, paramString, null, paramITaskListener);
    }

    public void execute(int paramInt, String paramString1, String paramString2, ITaskListener paramITaskListener) {
        this.listener = paramITaskListener;
        this.timeOut = paramInt;
        this.sessionId = paramString2;
        this.url = paramString1;
        setDaemon(true);
        start();
    }

    public void execute(String paramString, ITaskListener paramITaskListener) {
        execute(30, paramString, paramITaskListener);
    }

    public void run() {
        this.latch = new CountDownLatch(1);
        new Thread() {
            public void run() {
                try {
                    if (HttpTask.this.latch.await(HttpTask.this.timeOut, TimeUnit.SECONDS)) {
                        if (!HttpTask.this.isException)
                            HttpTask.this.listener.onReciveResponse(HttpTask.this.sessionId, HttpTask.this.content);
                    } else {
                        HttpTask.this.isTimeout = true;
                        HttpTask.this.listener.onTimeOut();
                        interrupt();
                        HttpTask.this.client.getConnectionManager().shutdown();
                    }
                } catch (InterruptedException localInterruptedException) {
                }
            }
        }.start();
        this.client = new DefaultHttpClient();
        this.client.getConnectionManager().closeIdleConnections(this.timeOut, TimeUnit.SECONDS);
        HttpGet localHttpGet = new HttpGet(this.url);
        try {
            HttpEntity localHttpEntity = this.client.execute(localHttpGet).getEntity();
            if (localHttpEntity != null) {
                InputStream localInputStream = localHttpEntity.getContent();
                ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
                byte[] arrayOfByte = new byte[500];
                int i;
                while ((i = localInputStream.read(arrayOfByte)) >= 0) {
                    if ((this.isStop) || (this.isTimeout))
                        return;
                    localByteArrayOutputStream.write(arrayOfByte, 0, i);
                }
                this.content = localByteArrayOutputStream.toByteArray();
                this.latch.countDown();
            }
            if ((!this.isStop) && (!this.isTimeout)) {
                this.isException = true;
                this.latch.countDown();
                this.listener.onException(new Exception("服务器错误，无数据返回！"));
            }
        } catch (ClientProtocolException localClientProtocolException) {
            localClientProtocolException.printStackTrace();
            if ((!this.isStop) && (!this.isTimeout)) {
                this.isException = true;
                this.latch.countDown();
                this.listener.onException(new Exception("服务器错误，服务器无响应！"));
            }
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
            if ((!this.isStop) && (!this.isTimeout)) {
                this.isException = true;
                this.latch.countDown();
                this.listener.onException(new Exception("连接错误，网络连接已断开！"));
            }
        } catch (Exception localException) {
            localException.printStackTrace();
            if ((!this.isStop) && (!this.isTimeout)) {
                this.isException = true;
                this.latch.countDown();
                this.listener.onException(new Exception("服务器错误，其他未知错误！"));
            }
        }
    }

    public void stopTask() {
        this.isStop = true;
    }
}

/* Location:           F:\temp\tools\classes_dex2jar.jar
 * Qualified Name:     com.rui.net.http.HttpTask
 * JD-Core Version:    0.6.0
 */